package com.wjw.java.io;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 文件拷贝:将f盘的aaa.jpg拷贝到e盘
 */
public class FileCopy {

    public static void main(String[] args) {
        String srcFilePath = "f:\\IO\\map.jpg";
        String destFilePath = "f:\\IO\\map2.jpg";

        FileInputStream fis = null;
        FileOutputStream fos = null;

        try {
            fis = new FileInputStream(srcFilePath);
            fos = new FileOutputStream(destFilePath);
            int readLength = 0;
            byte [] buff = new byte[1024];
            //一边读取文件,一边拷贝
            while ((readLength = fis.read(buff)) != -1){
                fos.write(buff,0,readLength);//一定要用这个方法
                //fos.write(buff);//如果使用这个方法,循环读取的长度不够时,文件会丢失
            }
            System.out.println("拷贝成功~");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fis != null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(fos != null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
